import 'package:amway_superapp_creator_plugin/models/choose_shop_bean.dart';
import 'package:amway_superapp_creator_plugin/models/search_shop.dart';
import 'package:amway_superapp_creator_plugin/models/shop_list_model.dart';
import 'package:amway_superapp_creator_plugin/net/http_manager.dart';
import 'package:amway_superapp_creator_plugin/viewmodels/base_model.dart';
import 'package:flutter/material.dart';

import '../../api.dart';
import '../../view_state.dart';

class ShopModel extends BaseModel {
  ShopModel({@required Api api}) : super(api: api);
  List<ChooseShopBean> _category = [];

  List<ChooseShopBean> get category => _category;

  List<ShopListBean> _shop = [];

  List<ShopListBean> get shop => _shop;

  void getCategoryList() {
    HttpManager().get(
        url: "apis/v1/creator/product/categoryList",
        tag: null,
        successCallback: (data) {
          if (data != null) {
            for (var v in data) {
              _category.add(ChooseShopBean.fromJson(v));
            }
          }
          setState(ViewState.Idle);
        },
        errorCallback: (data) {
          setState(ViewState.Idle);
          _shop = [];
        });
  }

  void getShopList(SearchShop data) {
    HttpManager().post(
        url: "apis/v1/creator/product/search",
        data: data,
        tag: null,
        successCallback: (data) {
          if (data != null) {
            for (var v in data) {
              _shop.add(ShopListBean.fromJson(v));
            }
          }
          setState(ViewState.Idle);
        },
        errorCallback: (data) {
          setState(ViewState.Idle);
          _shop = [];
        });
  }
}
